using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using Framework.Library;
using Framework.Library.Components;
using Framework.Library.Utilities;
using Framework.Library.Helper;

namespace Framework.Module.Adsvertising.Lib.Zones
{
    public class ConstantsCMS
    {
        private const string CONFIG_FILENAME = "/Modules/CMS/CMS.config";
        public static double CacheExpire_Zone
        {
            get { return ConvertUtility.ToDouble(AppEnv.GetAppSetting("CacheExpire_Zone", CONFIG_FILENAME)); }
        }
    }

	public class ZoneController
	{
        #region BackEnd
		private const string key = "Framework.Portal.CMS.Zone";
        public static DataTable GetZones(int parentId, string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(AppEnv.ConnectionString, "CMS_Zones_GetByParentID", parentId, lang);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public static DataTable GetZones(int parentId)
        {
            string param = "GetZones?" + parentId.ToString() + "&" + AppEnv.GetLanguage();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = GetZones(parentId, AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
		public static void LoadZones(ListItemCollection lstItems)
		{
			DataTable dtZones = GetZones(0);
			foreach(DataRow row in dtZones.Rows)
			{
				ListItem item = new ListItem(row["Zone_Name"].ToString(), row["Zone_ID"].ToString());
				item.Attributes.Add("Level", "0");
				lstItems.Add(item);
				LoadForCurItem(lstItems, item);
			}
		}
        public static void LoadZones(ListItemCollection lstItems, string portalid)
        {
            DataTable dtZones = GetZones(0, portalid);
            foreach (DataRow row in dtZones.Rows)
            {
                ListItem item = new ListItem(row["Zone_Name"].ToString(), row["Zone_ID"].ToString());
                item.Attributes.Add("Level", "0");
                lstItems.Add(item);
                LoadForCurItem(lstItems, item);
            }
        }
		public static void LoadForCurItem(ListItemCollection lstItems, ListItem curItem)
		{
			int curId = ConvertUtility.ToInt32(curItem.Value);
			int level = ConvertUtility.ToInt32(curItem.Attributes["Level"]);
			level++;
			DataTable dtChildZones = GetZones(curId);
			foreach(DataRow row in dtChildZones.Rows)
			{
				ListItem item = new ListItem(MiscUtility.StringIndent(level) + row["Zone_Name"].ToString(), row["Zone_ID"].ToString());
				item.Attributes.Add("Level", level.ToString());
				lstItems.Add(item);
				LoadForCurItem(lstItems, item);
			}
        }

        #endregion

    }
}
